from flask import Blueprint, jsonify, request, g
from app.err_no import errno_no
from loguru import logger
import json
import traceback
from app.db.mongoClient import MongoError
from app.db import dbServerManage

blueprint = Blueprint('server_manage', __name__, url_prefix='/api/server')


@blueprint.route('/v1/edit_schoolNameLogo', methods=['POST'])
def edit_schoolNameLogo():
    '''
    设置学校名称及logo
    '''
    try:
        # 如果不是超级管理员则无权限操作
        if g.userType != 'all':
            return jsonify(errno_no.ErrServerPermissionDenied.data(None))

        j_data = json.loads(request.data)
        schoolName = j_data.get('schoolName')
        schoolLogo = j_data.get('schoolLogo')
        if schoolName is None or len(schoolName) <= 0 \
            or schoolLogo is None:
            return jsonify(errno_no.ErrServerParam.data(None))

        dbServerManage.editSchoolNameLogo(schoolName, schoolLogo)

        return jsonify(errno_no.OK.data(None))
    except MongoError as e:
        logger.error(f'操作mongo异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))
    except Exception as e:
        logger.error(f'业务异常:{e.__dict__},traceback={traceback.format_exc()}')
        return jsonify(errno_no.ErrServerRuntime.data(None))